﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Corona
{
	/// <summary>
	/// Selector.xaml 的交互逻辑
	/// </summary>
	public partial class Selector
	{
        ///// <summary>
        ///// 取点间距，即鼠标相距多大才会取一个顶点加到多边形中
        ///// </summary>
        public static double DrawSpacing = 10;
        /// <summary>
        /// 边线的宽度
        /// </summary>
        public double SideLineThickness = 2;
        /// <summary>
        /// 选区的透明度
        /// </summary>
        public double SelectorOpacity = 0.2;
        /// <summary>
        /// 边线的颜色
        /// </summary>
        public Color SideLineColor
        {
            get { return (Color)GetValue(SideLineColorProperty); }
            set { SetValue(SideLineColorProperty, value); }
        }
        public static readonly DependencyProperty SideLineColorProperty = DependencyProperty.Register("SideLineColor", typeof(Color), typeof(Selector), new FrameworkPropertyMetadata(SideLineColorPropertyChanged));
        private static void SideLineColorPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as Selector).SideLineBrush.Color = (source as Selector).SideLineColor;
        }
        /// <summary>
        /// 选区的颜色
        /// </summary>
        public Color SelectorColor
        {
            get { return (Color)GetValue(SelectorColorProperty); }
            set { SetValue(SelectorColorProperty, value); }
        }
        public static readonly DependencyProperty SelectorColorProperty = DependencyProperty.Register("SelectorColor", typeof(Color), typeof(Selector), new FrameworkPropertyMetadata(SelectorColorPropertyChanged));
        private static void SelectorColorPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as Selector).SelectorBrush.Color = (source as Selector).SelectorColor;
        }
        private SolidColorBrush SideLineBrush = new SolidColorBrush();
        private SolidColorBrush SelectorBrush = new SolidColorBrush();

        /// <summary>
        /// 是否可以绘制选区
        /// </summary>
        private bool IsDrawing = false;

        /// <summary>
        /// 初始点
        /// </summary>
        private Point initialPoint;

        /// <summary>
        /// 起始点
        /// </summary>
        private Point startPoint;

        /// <summary>
        /// 结束点
        /// </summary>
        private Point endPoint;

        /// <summary>
        /// 终结点
        /// </summary>
        private Point finalPoint;

        /// <summary>
        /// 用于记录多边形选区的所有顶点
        /// </summary>
        public PointCollection SelectorPoints = new PointCollection();


        /// <summary>
        /// 添加顶点时发出的事件
        /// </summary>
        /// <param name="sender"></param>
        public delegate void AddSelectorPointHandler(object sender);
        public event AddSelectorPointHandler AddSelectorPoint;

        //保留字段，用于获取选区大小
        //private double MostLeft;
        //private double MostRight;
        //private double MostTop;
        //private double MostBottom;

		public Selector()
		{
			this.InitializeComponent();
		}
        /// <summary>
        /// 绘制选区初始化
        /// </summary>
        /// <param name="InitialPoint"></param>
        public void InitialSelector(Point InitialPoint)
        {
            SelectorPoints.Clear();
            IsDrawing = true;
            initialPoint = InitialPoint;
            startPoint = InitialPoint;
            SelectorPoints.Add(initialPoint);
            //以下为为计算选区中心做准备的过程
            //MostLeft = MostRight = startPoint.X;
            //MostTop = MostBottom = startPoint.Y;
        }

        /// <summary>
        /// 绘制选区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrawSelector(object sender, MouseEventArgs e)
        {
            Canvas canv = sender as Canvas;
            endPoint = e.GetPosition(canv);
            if (IsDrawing && Math.Sqrt(Math.Pow(startPoint.X - endPoint.X, 2) + Math.Pow(startPoint.Y - endPoint.Y, 2)) >= DrawSpacing)
            {
                //多边形中添加新的点
                SelectorPoints.Add(endPoint);
                //发出添加新点的事件
                if (AddSelectorPoint != null)
                    AddSelectorPoint(this);
                //绘制边线
                Line line = new Line();
                line.X1 = startPoint.X;
                line.Y1 = startPoint.Y;
                line.X2 = endPoint.X;
                line.Y2 = endPoint.Y;
                line.StrokeThickness = SideLineThickness;
                line.Stroke = SideLineBrush;
                canv.Children.Add(line);

                //绘制三角形
                Polygon poly = new Polygon();
                //poly.Points = SelectorPoints;
                poly.Points.Add(initialPoint);
                poly.Points.Add(startPoint);
                poly.Points.Add(endPoint);
                poly.Fill = Brushes.AliceBlue;
                poly.Opacity = 0.2;
                canv.Children.Add(poly);

                //将Start点设为End点，构成链
                startPoint = endPoint;

                //以下为为计算选区中心做准备的过程
                //if (startPoint.X > MostRight)
                //    MostRight = startPoint.X;
                //else if (startPoint.X < MostLeft)
                //    MostLeft = startPoint.X;
                //if (startPoint.Y > MostBottom)
                //    MostBottom = startPoint.Y;
                //else if (startPoint.Y < MostTop)
                //    MostTop = startPoint.Y;
            }
        }

        /// <summary>
        /// 结束绘制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EndDrawSelector(object sender, MouseButtonEventArgs e)
        {
            Canvas canv = sender as Canvas;
            finalPoint = e.GetPosition(canv);
            SelectorPoints.Add(finalPoint);
            if (AddSelectorPoint != null)
                AddSelectorPoint(this);

            IsDrawing = false;

            //绘制最后一条边线
            Line line = new Line();
            line.X1 = initialPoint.X;
            line.Y1 = initialPoint.Y;
            line.X2 = finalPoint.X;
            line.Y2 = finalPoint.Y;
            line.Stroke = SideLineBrush;
            line.StrokeThickness = SideLineThickness;
            canv.Children.Add(line);

            //将最后一点和首点连接起来，构成封闭区域
            Line linef = new Line();
            linef.X1 = finalPoint.X;
            linef.Y1 = finalPoint.Y;
            linef.X2 = startPoint.X;
            linef.Y2 = startPoint.Y;
            linef.Stroke = SideLineBrush;
            linef.StrokeThickness = SideLineThickness;
            canv.Children.Add(linef);

            //绘制最后一块三角形
            Polygon poly = new Polygon();
            poly.Points.Add(initialPoint);
            poly.Points.Add(startPoint);
            poly.Points.Add(finalPoint);
            poly.Fill = Brushes.AliceBlue;
            poly.Opacity = 0.2;
            canv.Children.Add(poly);
        }

        /// <summary>
        /// 清除Canvas上的所有元素
        /// </summary>
        public void ClearSelector()
        {
            ParentCanvas.Children.Clear();
        }

        /// <summary>
        /// 颜色缺省设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectorControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.SideLineColor == null)
            {
                this.SideLineColor = Colors.Azure;
            }
            if (this.SelectorColor == null)
            {
                this.SelectorColor = Colors.Azure;
            }
        }
	}
}